import json
import os
import smtplib

from email.mime.text import MIMEText
from email.header import Header

current_directory = os.path.dirname(os.path.abspath(__file__))

with open(current_directory + "/mail_config.json", "r") as f:
    data = json.load(f)

mail_host = data.get("mail_host")  # 设置服务器
mail_user = data.get("mail_user")  # 用户名
mail_pass = data.get("mail_pass")  # 口令
sender = data.get("sender")

default_name = 'Bot-Diana'
default_sub = '体温填报通知'
default_receiver_name = 'Anonymous-User'
default_text = '默认文本'


def send_temp_state(
        receiver: str,
        receiver_name: str,
        is_login: bool,
        is_ok: bool,
        temp: str = "error"
        ):
    """
    发送体温填报通知邮件
    :param receiver: 接受者邮箱
    :param receiver_name: 接受者昵称
    :param is_login: 科师空间是否登录成功
    :param is_ok: 体温是否上报成功
    :param temp: 上报的体温数值
    :return: 无
    """
    if is_ok:
        send_mail(receiver, f"体温上报成功，上报温度为{temp}", receiver_name, default_name, default_sub)
    elif is_login:
        send_mail(receiver, "体温上报失败，请检查服务器是否正常运行", receiver_name, default_name, default_sub)
    else:
        send_mail(receiver, "体温上报失败，请检查账号密码", receiver_name, default_name, default_sub)


def send_registry_mail(
        receiver: str,
        password: str,
        receiver_name: str = default_receiver_name,
        sender_name: str = default_name,
        subject: str = 'Project Sympathy 注册成功通知'
        ):
    """
    发送注册成功通知
    :param receiver: 接受者邮箱
    :param password: 初始密码
    :param receiver_name: 接受者昵称
    :param sender_name: 发送者昵称
    :param subject: 邮件标题
    :return: 无
    """
    text = f'您已成功在本站点注册，系统为您生成了初始密码，请使用邮箱登录\n您的密码为：{password}'
    send_mail(receiver, text=text, receiver_name=receiver_name, sender_name=sender_name, subject=subject)


def send_reset_mail(
        receiver: str,
        password: str,
        receiver_name: str = default_receiver_name,
        sender_name: str = default_name,
        subject: str = 'Project Sympathy 重置密码通知'
        ):
    text = f'您的密码已重置，请使用邮箱登录\n您的密码为：{password}'
    send_mail(receiver, text=text, receiver_name=receiver_name, sender_name=sender_name, subject=subject)


def send_mail(
        receiver: str,
        text: str = default_text,
        receiver_name: str = default_receiver_name,
        sender_name: str = default_name,
        subject: str = default_sub
        ):
    """
    向制定邮箱发送邮件
    :param receiver: 接受者邮箱
    :param text: 邮件正文
    :param receiver_name: 接受者昵称
    :param sender_name: 发送者昵称
    :param subject: 邮件标题
    :return: 无
    """
    message = get_message(text, receiver_name, sender_name, subject)

    smtpObj = smtplib.SMTP()
    smtpObj.connect(mail_host, 25)  # 25 为 SMTP 端口号
    smtpObj.login(mail_user, mail_pass)

    smtpObj.sendmail(sender, receiver, message.as_string())


def get_message(
        text: str,
        receiver_name: str,
        sender_name: str,
        sub: str,
        ):
    """
    格式化邮件内容
    :param text: 邮件正文
    :param receiver_name: 接受者昵称
    :param sender_name: 发送者昵称
    :param sub: 邮件标题
    :return: 格式化内容
    """
    message = MIMEText(f'{text}', 'plain', 'utf-8')
    message['From'] = Header(sender_name, 'utf-8')
    message['To'] = Header(f"{receiver_name}", 'utf-8')
    subject = sub
    message['Subject'] = Header(subject, 'utf-8')
    return message

